<template>
  <div class="admin-role-management">
    <el-card>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="角色名称">
          <el-input
            v-model="searchForm.roleName"
            placeholder="请输入角色名称"
          />
        </el-form-item>
        <el-form-item label="角色标识">
          <el-input
            v-model="searchForm.roleIdentifier"
            placeholder="请输入角色标识"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">筛选</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="角色ID" width="80" />
        <el-table-column prop="roleName" label="角色名称" width="120" />
        <el-table-column prop="roleIdentifier" label="角色标识" width="120" />
        <el-table-column prop="remarks" label="备注" />
        <el-table-column prop="createdAt" label="创建时间" width="160" />
        <el-table-column prop="updatedAt" label="修改时间" width="160" />
        <el-table-column label="操作" width="250">
          <template #default="scope">
            <el-button
              size="small"
              type="primary"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
            <el-button
              size="small"
              type="warning"
              @click="handleAssignPermissions(scope.row)"
              >权限分配</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-button type="primary" @click="handleAddAccount">添加账号</el-button>
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

const searchForm = reactive({
  roleName: "",
  roleIdentifier: "",
});

const tableData = ref([
  {
    id: 5,
    roleName: "开发者",
    roleIdentifier: "dev",
    remarks: "开发者",
    createdAt: "2020-07-14 23:34:09",
    updatedAt: "2021-08-10 01:57:21",
  },
  {
    id: 3,
    roleName: "游客",
    roleIdentifier: "guest",
    remarks: "游客",
    createdAt: "2020-02-26 15:19:49",
    updatedAt: "2020-07-14 23:33:55",
  },
  {
    id: 2,
    roleName: "普通用户",
    roleIdentifier: "user",
    remarks: "普通用户",
    createdAt: "2020-02-26 15:18:52",
    updatedAt: "2021-08-10 01:55:17",
  },
  {
    id: 1,
    roleName: "管理员",
    roleIdentifier: "admin",
    remarks: "管理员",
    createdAt: "2020-02-26 15:18:37",
    updatedAt: "2020-03-21 15:15:54",
  },
]);

const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(4);

const onSearch = () => {
  // Implement search logic here
  console.log("Searching with:", searchForm);
  ElMessage.success("搜索完成");
};

const handleEdit = (row) => {
  console.log("Editing:", row);
  ElMessage.info(`编辑角色: ${row.roleName}`);
};

const handleDelete = (row) => {
  ElMessageBox.confirm(`确定要删除角色 "${row.roleName}" 吗？`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      console.log("Deleting:", row);
      ElMessage.success(`删除角色 ${row.roleName} 成功`);
    })
    .catch(() => {
      ElMessage.info("已取消删除");
    });
};

const handleAssignPermissions = (row) => {
  console.log("Assigning permissions for:", row);
  ElMessage.info(`为角色 ${row.roleName} 分配权限`);
};

const handleAddAccount = () => {
  console.log("Adding new account");
  ElMessage.success("打开添加账号对话框");
};

const handleSizeChange = (val) => {
  console.log(`${val} items per page`);
  // Implement logic to fetch data with new page size
};

const handleCurrentChange = (val) => {
  console.log(`Current page: ${val}`);
  // Implement logic to fetch data for the new page
};
</script>

<style scoped>
.admin-role-management {
  padding: 20px;
}
.pagination-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
}
</style>
